package com.hshx.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hshx.pojo.Train;
import com.hshx.service.TrainService;
import com.hshx.utils.ResultObj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

@CrossOrigin
@RequestMapping("/trainService")
@RestController
public class TrainController {
    @Autowired
    private TrainService trainService;

/*    @RequestMapping("/getTrainPage")
    public Map<String,Object> getTrainPage(Integer pageIndex, Integer pageSize){
        HashMap<String, Object> trainMap = new HashMap<>();
        if(pageIndex==null||pageIndex<=0){
            pageIndex=1;
        }
        if(pageSize==null||pageSize<=0){
            pageSize=10;
        }
        List<Object> objects = redisUtil1.lRange("trainAll",0,-1);
        if(objects!=null&&objects.size()>0){
            System.out.println("redis中存在");

            List<Train> trainList= objects.stream().map(o->((Train)o)).collect(Collectors.toList());
            trainMap.put("pageSize",pageSize);
            trainMap.put("pages", trainList.size()%pageSize==0?trainList.size()/pageSize:trainList.size()/pageSize+1);
            trainMap.put("pageNum",pageIndex);
            trainMap.put("list", trainList.stream().sorted(Comparator.comparing(Train::getTrain_id).reversed()).skip((pageIndex-1)*pageSize).limit(pageSize).collect(Collectors.toList()));
            trainMap.put("total", trainList.size());
        }else{//redis中不存在或失效
            System.out.println("redis中不存在或失效");

            Page trainPage= PageHelper.startPage(pageIndex,pageSize);
            List<Train> trainList=trainService.list(new QueryWrapper<Train>().orderByDesc("train_id"));
            redisUtil1.lPushAll("trainAll",trainList);
            trainMap.put("pageSize",trainPage.getPageSize());
            trainMap.put("pages", trainPage.getPages());
            trainMap.put("pageNum",trainPage.getPageNum());
            trainMap.put("list", trainPage.getResult());
            trainMap.put("total", trainPage.getTotal());
        }

        return trainMap;
    }*/
    //添加培训记录
    @RequestMapping("/addTrain")
    @ResponseBody
    public boolean addTrain(Train train){
        if (trainService.addTrain(train)>0){
            return true;
        }
        return false;
    }

    /**
     * 分页查询
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/listPage")
    @ResponseBody
    public HashMap<String, Object> listPage(Integer page, Integer limit) {
        HashMap<String,Object> hashMap = new HashMap<>();
    //分页参数
    Page<Train> objectPage = new Page<>(page, limit);
    QueryWrapper<Train> queryWrapper = new QueryWrapper<>();
    //分页查询
    IPage<Train> tempPage = trainService.page(objectPage, queryWrapper);
        hashMap.put("count",tempPage.getTotal());
        hashMap.put("data",tempPage.getRecords());
        hashMap.put("code",0);
        return hashMap;
//    return new DataGrid(tempPage.getTotal(), tempPage.getRecords());
}

    /**
     * 按条件查询
     * @param page
     * @param limit
     * @param training_course
     * @param trainer
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public HashMap<String, Object> list(Integer page, Integer limit, String training_course, String trainer) {
        HashMap<String,Object> hashMap = new HashMap<>();
        //分页参数
        Page<Train> objectPage = new Page<>(page, limit);
        QueryWrapper<Train> queryWrapper = new QueryWrapper<>();
        //条件查询
//        queryWrapper.orderByDesc("create_time");//根据新增时间降序
        queryWrapper.like(StringUtils.isNotBlank(training_course), "TRAINING_COURSE", training_course);
        queryWrapper.like(StringUtils.isNotBlank(trainer), "TRAINER", trainer);
        //分页查询
        IPage<Train> tempPage = trainService.page(objectPage, queryWrapper);
        hashMap.put("count",tempPage.getTotal());
        hashMap.put("data",tempPage.getRecords());
        hashMap.put("code",0);;
        return hashMap;
    }

    /**
     * 根据id修改状态
     * @param train_id
     * @param train_state
     * @return
     */
    @RequestMapping("/updateTrainState")
    @ResponseBody
    public boolean updateTrainState(Integer train_id,Integer train_state){
        if (trainService.updateTrainState(train_id, train_state)>0){
            return true;
        }
        return false;
    }
    /**
     * 按id查询
     * @param train_id
     * @return
     */
    @RequestMapping("/trainID")
    @ResponseBody
    public Train trainID(Integer train_id){
        System.out.println("ssssssss:"+trainService.queryTrainID(train_id));
        Train train = trainService.queryTrainID(train_id);
        return train;
    }


    /**
     * 修改培训记录
     * @param train
     * @return
     */
    @RequestMapping("/updateTrain")
    public boolean updateTrain(Train train){
        if (trainService.updateTrain(train)>0){
            return true;
        }
        return false;
    }

    /**
     * 根据pmsLineId回显到修改页面
     *
     * @param train_id
     * @param model
     * @return
     */
    @RequestMapping("/toEdit")
    public String toEdit(@RequestParam String train_id, Model model) {
        Train trainById = trainService.getById(train_id);
        model.addAttribute("train", trainById);
        return "update";
    }

    /**
     * 根据pmsLineId单个删除
     *
     * @param train_id
     * @return
     */
    @RequestMapping("/delete")
//    @ResponseBody
    public ResultObj delete(Integer train_id) {
        try {
            if (!"".equals(train_id)) {
                this.trainService.removeById(train_id);
            }
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 根据ids批量删除
     *
     * @param ids
     * @return
     */
    @RequestMapping("/delByIds")
    @ResponseBody
    public ResultObj removeByIds(String ids) {
        System.out.println(ids);
        try {
            if (!"".equals(ids)) {
                String[] str = ids.split(",");
                List<String> list = Arrays.asList(str);
                for (String idList : list) {
                    this.trainService.removeByIds(Collections.singleton(idList));
                }
            }
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }
}
